package main

import (
	"context"
	"fmt"
	"gitee.com/gopher2011/go-grpc-test/server/services"
	"google.golang.org/grpc"
	"google.golang.org/grpc/credentials"
	"log"
)

func main(){
	// 不添加证书连接服务器
	//conn,err := grpc.Dial(":8081",grpc.WithInsecure()) // 第一个参数: 目标服务器地址,
	//if err != nil {
	//	log.Fatalln(err)
	//}

	// 客户端添加证书连接服务器
	creds,err := credentials.NewClientTLSFromFile("client/keys/server.crt","sensetime.com")
	if err != nil {
		log.Fatalf("加载客户端证书失败，err: %v",err.Error())
	}
	conn,err := grpc.Dial(":8081",grpc.WithTransportCredentials(creds))
	if err !=nil {
		log.Fatalf("连接GRPC服务端失败 %v\n",err.Error())
	}

	defer conn.Close()
	// 注册客户端
	prodClient := services.NewProductServiceClient(conn)

	// 执行具体的业务逻辑
	prodRes,err := prodClient.GetProductStock(context.Background(),&services.ProdRequest{ProdId: 20})
	if err != nil {
		log.Fatalf("请求GRPC服务端失败 %v\n",err.Error())
	}
	fmt.Println(prodRes.ProdStock)
}




